package org.road0329.tools.proto_parse_tool.ems;

import org.road0329.tools.proto_parse_tool.enums.DataItemEnum;
import org.road0329.tools.proto_parse_tool.enums.EnergyTypeEnum;
import org.road0329.tools.proto_parse_tool.enums.EventStatusEnum;
import org.road0329.tools.proto_parse_tool.enums.ObjectTypeEnum;

import java.math.BigDecimal;
import java.util.Date;

public class ProtocolWaterUtil {
    private static final short EVENT_ITEM_BATTERY_UNDERVOLTAGE = 201;//电池欠压

    public static DataEvent buildEvent(Integer pointId, Date dataTime) {
        DataEvent event = new DataEvent();
        event.setObjId(pointId);
        event.setObjType(ObjectTypeEnum.OBJ_POINT.getShortValue());
        event.setEventItem(EVENT_ITEM_BATTERY_UNDERVOLTAGE);
        event.setEventData("电池欠压事件");
        event.setEventNote("电池欠压事件");
        event.setEventTime(dataTime);
        event.setStatus(EventStatusEnum.HAPPEN.getShortValue());
        Date now = new Date();
        event.setUpdateTime(now);
        return event;
    }


    public static DataCumulant buildCumulant(Integer pointId, BigDecimal value, Short status, Date dataTime) {
        DataCumulant cumulant = new DataCumulant();
        cumulant.setPointId(pointId);
        cumulant.setDataItem(DataItemEnum.WATER_BM.getShortValue());
        cumulant.setEnergyType(EnergyTypeEnum.WATER.getShortValue());
        cumulant.setValue(value);
        cumulant.setDataTime(dataTime);
        cumulant.setBatteryStatus(status);

        Date now = new Date();
        cumulant.setAddTime(now);
        cumulant.setUpdateTime(now);
        return cumulant;
    }

    public static DataInstant buildInstant(Integer pointId, short dataItem, BigDecimal value, Date dataTime) {
        DataInstant instant = new DataInstant();
        Date now = new Date();
        instant.setAddTime(now);
        instant.setUpdateTime(now);

        instant.setPointId(pointId);
        instant.setDataItem(dataItem);
        instant.setValue(value);
        instant.setDataTime(dataTime);
        return instant;
    }

    public static short getStatus(byte statusST) {
        short status = 0;
        if ((statusST & 0x03) == 0x00) {//阀门开
            status = 0;
        } else if ((statusST & 0x03) == 0x01) {//关
            status = 1;
        } else {//异常
            status = 3;
        }
        return status;
    }
}
